package com.example.petshopping.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.model.Product;
import com.example.petshopping.model.Store;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺列表适配器
 */
public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.StoreViewHolder> {
    
    private final Context context;
    private final List<Store> stores;
    private final OnStoreClickListener listener;
    
    public interface OnStoreClickListener {
        void onStoreClick(Store store);
        void onEnterStoreClick(Store store);
    }
    
    public StoreAdapter(Context context, List<Store> stores, OnStoreClickListener listener) {
        this.context = context;
        this.stores = stores != null ? stores : new ArrayList<>();
        this.listener = listener;
    }
    
    @NonNull
    @Override
    public StoreViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_store, parent, false);
        return new StoreViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull StoreViewHolder holder, int position) {
        Store store = stores.get(position);
        holder.bind(store);
    }
    
    @Override
    public int getItemCount() {
        return stores.size();
    }
    
    public void updateData(List<Store> newStores) {
        this.stores.clear();
        if (newStores != null) {
            this.stores.addAll(newStores);
        }
        notifyDataSetChanged();
    }
    
    class StoreViewHolder extends RecyclerView.ViewHolder {
        private final ImageView ivStoreAvatar;
        private final TextView tvStoreName;
        private final TextView tvStoreRating;
        private final TextView tvStoreFollowers;
        private final Button btnEnterStore;
        private final ImageView ivProduct1, ivProduct2, ivProduct3, ivProduct4;
        private final TextView tvProductPrice1, tvProductPrice2, tvProductPrice3, tvProductPrice4;
        
        public StoreViewHolder(@NonNull View itemView) {
            super(itemView);
            ivStoreAvatar = itemView.findViewById(R.id.ivStoreAvatar);
            tvStoreName = itemView.findViewById(R.id.tvStoreName);
            tvStoreRating = itemView.findViewById(R.id.tvStoreRating);
            tvStoreFollowers = itemView.findViewById(R.id.tvStoreFollowers);
            btnEnterStore = itemView.findViewById(R.id.btnEnterStore);
            ivProduct1 = itemView.findViewById(R.id.ivProduct1);
            ivProduct2 = itemView.findViewById(R.id.ivProduct2);
            ivProduct3 = itemView.findViewById(R.id.ivProduct3);
            ivProduct4 = itemView.findViewById(R.id.ivProduct4);
            tvProductPrice1 = itemView.findViewById(R.id.tvProductPrice1);
            tvProductPrice2 = itemView.findViewById(R.id.tvProductPrice2);
            tvProductPrice3 = itemView.findViewById(R.id.tvProductPrice3);
            tvProductPrice4 = itemView.findViewById(R.id.tvProductPrice4);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onStoreClick(stores.get(position));
                }
            });
            
            btnEnterStore.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onEnterStoreClick(stores.get(position));
                }
            });
        }
        
        public void bind(Store store) {
            // 设置店铺信息
            tvStoreName.setText(store.getName());
            tvStoreRating.setText(store.getRating());
            tvStoreFollowers.setText(store.getFollowers());
            
            // 这里应该使用图片加载库加载网络图片，这里简单使用资源图片
            ivStoreAvatar.setImageResource(R.drawable.yingwu);
            
            // 设置产品信息
            List<Product> products = store.getProducts();
            if (products != null && products.size() >= 4) {
                // 产品1
                setProductImage(ivProduct1, products.get(0).getImageUrl());
                tvProductPrice1.setText(products.get(0).getPrice());
                
                // 产品2
                setProductImage(ivProduct2, products.get(1).getImageUrl());
                tvProductPrice2.setText(products.get(1).getPrice());
                
                // 产品3
                setProductImage(ivProduct3, products.get(2).getImageUrl());
                tvProductPrice3.setText(products.get(2).getPrice());
                
                // 产品4
                setProductImage(ivProduct4, products.get(3).getImageUrl());
                tvProductPrice4.setText(products.get(3).getPrice());
            }
        }
        
        private void setProductImage(ImageView imageView, String imageUrl) {
            // 这里应该使用图片加载库加载网络图片，现在先使用本地资源
            // 根据imageUrl来设置对应的本地资源图片
            switch (imageUrl) {
                case "yugang":
                    imageView.setImageResource(R.drawable.yugang);
                    break;
                case "pet_2":
                    // 这里使用重命名后的资源
                    imageView.setImageResource(R.drawable.pet_2);
                    break;
                case "pet_4":
                    imageView.setImageResource(R.drawable.pet_4);
                    break;
                case "pet_5":
                    imageView.setImageResource(R.drawable.pet_5);
                    break;
                default:
                    // 默认图片
                    imageView.setImageResource(R.drawable.yugang);
                    break;
            }
        }
    }
} 